package com.heima.controller;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se){
        //session创建了，那么就上线了
        //获得OlineCount的值，+1
        HttpSession session = se.getSession();
        Integer onlineCount  =(Integer) session.getServletContext().getAttribute("onlineCount");
        onlineCount ++;
        session.getServletContext().setAttribute("onlineCount",onlineCount);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se){
        //session销毁了，那么就离线了
        //获得OlineCount的值，-1
        HttpSession session = se.getSession();
        Integer onlineCount  =(Integer) session.getServletContext().getAttribute("onlineCount");
        onlineCount --;
        session.getServletContext().setAttribute("onlineCount",onlineCount);
    }

}
